#include "april/RenderSystem.h"
#include "april/Timer.h"
#include "aprilutil/StaticMesh.h"
#include "math.h"
#include "askal/AnimableObject.h"
#include "askal/Skeleton.h"

#include "gtypes/Matrix3.h"
#include "gtypes/Vector3.h"
#include "gtypes/Quaternion.h"
#include "askal/AskalDataCache.h"
#include <iostream>

askal::Skeleton skeleton;
askal::Bone b0,b1,b2,b3,b4,b5;

askal::AnimableObject objekt;

April::ColoredVertex grid[44];
float t;

void setupGrid(float spacing);

void drawGrid()
{
	April::rendersys->render(April::LineList, grid, 44);
}

bool render(float time_increase)
{
	April::rendersys->clear(true, false);
	t += time_increase;
	
	April::rendersys->setPerspective(60,800/600.,0.1f,100.0f);
	
	gvec3 pos(30, 12, 0);
	pos = gvec3(gquat::fromAxisAngle(0,1,0,t * 10) * gquat(pos, 0) * gquat::fromAxisAngle(0,1,0,t * 10).getConjugate());
	April::rendersys->lookAt(pos,gtypes::Vector3(0,0,0),gtypes::Vector3(0,1,0));
	drawGrid();
	
	/*objekt.update(time_increase);
	harray<April::PlainVertex> vs = objekt.getSkeletonVertexBatch();
	harray<April::PlainVertex> vm = objekt.getMeshVertexBatch();
	
	April::PlainVertex *vsd = new April::PlainVertex[vs.size()];
	for(int i = 0; i < vs.size(); ++i)
		vsd[i] = vs[i];
		
	April::PlainVertex *vmd = new April::PlainVertex[vm.size()];
	for(int i = 0; i < vm.size(); ++i)
		vmd[i] = vm[i];
		
	
	
	April::rendersys->render(April::LineList, vsd, vs.size());
	//April::rendersys->render(April::TriangleList, vmd, vm.size());
	April::rendersys->render(April::PointList, vmd, vm.size());*/
	
	return true;
}

int main()
{
	April::init("April",800,600,0,"AprilParticle Demo");
	April::rendersys->registerUpdateCallback(render);
	setupGrid(2.0);
	
	askal::AskalDataCache::Instance()->loadMeshAndSkeletonData("../../../media/save.amsh.txt");
	
	objekt.useSkeleton("Armature");
	objekt.useMesh("Cube");
	objekt.useTexture("");
	
	/*objekt.getAnimatonController()->playAnimation("Idle_Track");
	objekt.getAnimatonController()->setAnimationSpeed("Idle_Track", 0.5);
	objekt.getAnimatonController()->loopAnimation("Idle_Track");*/
	//objekt.getAnimatonController()->playAnimation("Middle_Finger_Up");
	//objekt.getAnimatonController()->setAnimationSpeed("Middle_Finger_Up", 0.1);
	objekt.getAnimatonController()->playAnimation("Middle_Finger_Waving");
	objekt.getAnimatonController()->setAnimationSpeed("Middle_Finger_Waving", 0.5);
	objekt.getAnimatonController()->loopAnimation("Middle_Finger_Waving");
	
	objekt.update(0.01);
	
	skeleton = *(askal::AskalDataCache::Instance()->getSkeleton("Armature"));
	/*std::cout << "Animacije : " << skeleton._mAnimations.size() << std::endl;
	for(hmap<hstr, askal::Animation>::iterator it = skeleton._mAnimations.begin(); it != skeleton._mAnimations.end(); it++)
	{
		std::cout << "Animacija : " << it->second.mAnimationName.c_str() << std::endl;
		for(hmap<hstr, harray<askal::Keyframe> >::iterator jt = it->second.mActions.begin(); jt != it->second.mActions.end(); jt++)
			std::cout << "\t Kljucevi : " << jt->first.c_str() << " - " << jt->second.size() << std::endl;
	}*/
    
	April::rendersys->enterMainLoop();
    
    
	April::destroy();
	
	return 0;
}

void setupGrid(float spacing)
{
	int i = 0;
	for(float s = -5*spacing; s <= 5*spacing; ++i, s += spacing)
	{
		April::ColoredVertex u,v,p,r;
		u.color = v.color = p.color = r.color = 0xFF222222;
		u.x = -5*spacing;
		u.y = 0;
		u.z = s;
		v.x = 5*spacing;
		v.y = 0;
		v.z = s;
		grid[i*4 + 0] = u;
		grid[i*4 + 1] = v;
		
		p.x = s;
		p.y = 0;
		p.z = -5*spacing;
		r.x = s;
		r.y = 0;
		r.z = 5*spacing;
		grid[i*4 + 2] = p;
		grid[i*4 + 3] = r;
	}
}
